package com.itzz.spring_zhoneHeng.controller;

import com.itzz.spring_zhoneHeng.base.RestResponse;
import com.itzz.spring_zhoneHeng.dao.MemberDao;
import com.itzz.spring_zhoneHeng.dao.UserDao;
import com.itzz.spring_zhoneHeng.pojo.Member;
import com.itzz.spring_zhoneHeng.pojo.User;
import com.itzz.spring_zhoneHeng.service.MemberService;
import com.itzz.spring_zhoneHeng.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/member")
public class MemberController {

    @Autowired
    UserService userService;
    @Autowired
    MemberService memberService;

    @Autowired
    MemberDao memberDao;


    //查询所有的会员(不分页)
    @RequestMapping("/queryAllMembers")
    public RestResponse list(){
        List<Member> list = memberDao.selectList(null);
        return RestResponse.ok(list);
    }


    //查询所有的会员(分页)
    @RequestMapping("/queryAll")
    public RestResponse list(User user,Integer pageNum,Integer pageSize){
        RestResponse response = userService.queryAll(user, pageNum, pageSize);
//
        return response;
    }

    //根据uid查询数据
    @RequestMapping("/queryById/{uid}")
    public RestResponse queryById(@PathVariable("uid") Integer uid){
        RestResponse response = memberService.queryById(uid);
        return response;
    }

    //修改数据
    @RequestMapping("/edit")
    public RestResponse edit(Integer mbid,Integer uid){
        RestResponse response = memberService.edit(mbid,uid);

        return response;
    }

    //根据id删除  member 表中的会员
    @RequestMapping("/delete/{id}")
    public RestResponse del(@PathVariable("id") Integer id){
        int i = memberDao.deleteById(id);
        if(i == 0){
            return RestResponse.fail(0,"删除失败");
        }
        return RestResponse.ok(i);
    }




    //根据删除数据  根据uid删除u_mb表数据(只是不显示了)
    @RequestMapping("/del/{id}")
    public RestResponse delete(@PathVariable("id") Integer uid){
        int i = memberDao.deleteU_MB(uid);
        return RestResponse.ok(i);
    }

    //添加会员
    @RequestMapping("/addMember")
    public RestResponse addUser(Member member){
        RestResponse response  = memberService.add(member);
        return response;
    }



}
